package com.boot.controller;

import java.util.ArrayList;
import java.util.List;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.boot.entity.TaskMessage;

@Controller
@RequestMapping("activiti")
public class ActivitiController {
	   @Autowired
	   private RepositoryService repositoryService;
	   @Autowired
	   private RuntimeService runtimeService;
	   @Autowired
	   private TaskService taskService;
      @RequestMapping("deployProcess")
      public @ResponseBody String deployProcess() {
    	 Deployment deploy = repositoryService.createDeployment().addClasspathResource("processes/MyProcess.bpmn")
    	  .addClasspathResource("processes/MyProcess.png").deploy();
    	 System.out.println("部署id为:"+deploy.getId());
    	 return "success";
      }
      
      @RequestMapping("startProcess")
      public @ResponseBody String startProcess() {
    	  runtimeService.startProcessInstanceById("myProcess:4:10006");
    	  return "启动流程成功";
      }
      
      
      @RequestMapping("handlerTask")
      public @ResponseBody String handlerTask() {
    	  taskService.complete("2504");
    	  return "handler success";
      }
       
      //测试流程定义的挂起，挂起流程定义之后，就不能启动这个流程了，如果启动这个流程，会抛出异常
      @RequestMapping("hangoutProcess")
      public @ResponseBody String hangoutProcess(){
    	  repositoryService.suspendProcessDefinitionById("myProcess:4:10006");
    	  return "流程定义挂起成功";
      }
      
      
     //流程定义的激活，用于激活被挂起的流程定义
      @RequestMapping("reliveProcess")
      public @ResponseBody String reliveProcess() {
    	  repositoryService.activateProcessDefinitionById("myProcess:4:10006");
    	  return "激活成功";
      }
      
      //查询api
      @RequestMapping("queryTask")
      public @ResponseBody List<TaskMessage> queryTask() {
    	  List<TaskMessage> list1 = new ArrayList<TaskMessage>();
    	  List<Task> list = taskService.createTaskQuery().taskAssignee("xiaoxia").orderByDueDate().asc().list();
    	  for(Task task:list) {
    		  TaskMessage msg = new TaskMessage();
    		  msg.setTaskId(task.getId());
    		  msg.setAssige(task.getAssignee());
    		  list1.add(msg);
    	  }
    	  Task task = list.get(0);
          System.out.println("hello");
    	  return list1;
      }
      
      
}
